package switchviz.graphic;

import java.math.BigDecimal;

import javax.media.opengl.GL;

import com.sun.opengl.util.GLUT;

public class GraphicUtils {

	public static int ALIGN_LEFT = 0;
	public static int ALIGN_RIGHT = 1;
	public static int ALIGN_CENTER = 2;
	
	public static int FONT_ROMAN = 0;
	
	public static void drawString(GL gl, int size, int alignment, int font, String string) 
	{
		GLUT glut = new GLUT();
		float width = glut.glutStrokeLength(font, string);
		float scalefactor = size/10000.0f;	
		
		gl.glScaled(scalefactor, scalefactor, 1.0f);
		
		switch (alignment)
		{
		case 0:
			drawString(glut, font, string);
			gl.glTranslatef(-width, 0, 0);
			break;
		case 1:
			gl.glTranslatef(-width, 0, 0);
			drawString(glut, font, string);
			break;
		case 2:
			gl.glTranslatef(-width/2, 0, 0);
			drawString(glut, font, string);
			gl.glTranslatef(-width/2, 0, 0);
			break;
		}
        
		gl.glScaled(1/scalefactor, 1/scalefactor, 1.0f);
    }
	
	public static void drawString(GL gl, int size, int alignment, int font, int number) 
	{
		String string = "";
		
		if (number >= 1000000000)
			string = (number/1000000000) + "G";
		else if (number >= 1000000)
			string = (number/1000000) + "M";
		else if (number >= 1000)
			string = (number/1000) + "K";
		else
			string = number + "";
		
		drawString(gl, size, alignment, font, string);
    }
	
	public static void drawString(GL gl, int size, int alignment, int font, double number) 
	{
		String string = "";
		
		if (number >= 1000000000)
			string = ((int)number/1000000000) + "G";
		else if (number >= 1000000)
			string = ((int)number/1000000) + "M";
		else if (number >= 1000)
			string = ((int)number/1000) + "K";
		else if (number < 1 && number > 0)
		{
			BigDecimal myDec = new BigDecimal( number );
			myDec = myDec.setScale(8, BigDecimal.ROUND_HALF_UP );
			string = myDec.toString();
			
			int i;
			
			for (i=string.length()-1; i>0; i--)
				if (!string.substring(i).startsWith("0"))
					break;
			
			string = string.substring(0, i+1);
		}
		else
			string = (int)number + "";
		
		drawString(gl, size, alignment, font, string);
    }
	
	private static void drawString(GLUT glut, int font, String string)
	{
		for (int i = 0; i < string.length(); i++) {

            char c = string.charAt(i);

            glut.glutStrokeCharacter(font, c);
        }
	}
}
